/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Hasil.java
 *
 * Created on 07 Sep 14, 21:40:18
 */
package com.pras.skripsi.view;

import com.pras.skripsi.Identity;
import com.pras.skripsi.Result;
import com.pras.skripsi.getResult;
import com.pras.skripsi.model.Entity;
import com.pras.skripsi.model.HasilTableModel;
import com.pras.skripsi.sql.DataPasienDao;
import com.pras.skripsi.sql.Database;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Pras
 */
public class Hasil extends javax.swing.JPanel {

    private HasilTableModel tableModel;
    private MainFrame frame;

    public Hasil(MainFrame frame) {
        this.frame = frame;
        initTable();
        initComponents();
        labNama.setText(Identity.getPasien().getNama());
        labTanggalLahir.setText(Identity.getPasien().getTanggalLahir());
        labJK.setText(Identity.getPasien().getJenisKelamin());
        labGD.setText(Identity.getPasien().getGolonganDarah());
        txtAlamat.setText(Identity.getPasien().getAlamat());
        labHasil.setText(new getResult().getResult());
    }

    public void initTable() {
        tableModel = new HasilTableModel();
        for (int i = 0; i < 2; i++) {
            Entity data = new Entity();
            data.setPlates((Integer) Result.getHasilAngka()[i][2]);
            data.setJawaban((String) Result.getHasilAngka()[i][1]);
            data.setKunci(Result.getKunci()[(Integer) Result.getHasilAngka()[i][0]]);
            tableModel.getList().add(data);
        }

        for (int i = 0; i < 2; i++) {
            Entity data = new Entity();
            data.setPlates((Integer) Result.getHasilBentuk()[i][2]);
            data.setJawaban((ImageIcon) Result.getHasilBentuk()[i][1]);
            data.setKunci((ImageIcon) Result.getKunci()[(Integer) Result.getHasilBentuk()[i][0]]);
            tableModel.getList().add(data);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        labNama = new javax.swing.JLabel();
        labHasil = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        labTanggalLahir = new javax.swing.JLabel();
        labJK = new javax.swing.JLabel();
        labGD = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAlamat = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        labHelp = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Nama :");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Hasil :");

        labNama.setText("jLabel3");

        labHasil.setText("jLabel4");

        jTable1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jTable1.setModel(tableModel);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.setAutoscrolls(false);
        jTable1.setColumnSelectionAllowed(true);
        jTable1.setFocusable(false);
        jTable1.setRequestFocusEnabled(false);
        jTable1.setRowSelectionAllowed(false);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jTable1.setUpdateSelectionOnSort(false);
        jTable1.setVerifyInputWhenFocusTarget(false);
        jTable1.setRowHeight(2, 100);
        jTable1.setRowHeight(3, 100);
        TableColumn column = null;
        for (int i = 0; i < 3; i++) {
            column = jTable1.getColumnModel().getColumn(i);
            if (i == 0) {
                column.setMinWidth(50);
                column.setMaxWidth(50);
                column.setPreferredWidth(50);
            } else {
                column.setMinWidth(200);
                column.setMaxWidth(200);
                column.setPreferredWidth(200);
            }
        }
        jScrollPane1.setViewportView(jTable1);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Alamat :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Jenis Kelamin :");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Golongan Darah :");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Tanggal Lahir :");

        labTanggalLahir.setText("jLabel7");

        labJK.setText("jLabel8");

        labGD.setText("jLabel9");

        txtAlamat.setEditable(false);
        txtAlamat.setColumns(20);
        txtAlamat.setRows(5);
        jScrollPane2.setViewportView(txtAlamat);

        jLabel10.setText("Rincian Hasil Jawaban :");

        labHelp.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        labHelp.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labHelp.setText("?");
        labHelp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labHelpMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                labHelpMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                labHelpMouseExited(evt);
            }
        });

        jButton1.setText("Simpan");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cetak");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labGD, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labJK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane2)
                            .addComponent(labTanggalLahir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labNama, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labHasil, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(labHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(labNama, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(labTanggalLahir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labJK)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labGD)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(labHasil, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labHelp))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(60, 60, 60)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(7, 7, 7)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Identity.getPasien().setHasil(labHasil.getText());
        Identity.getPasien().setTanggalTes(new java.util.Date());
        try {
            DataPasienDao dao = Database.getDataPasienDao();
            dao.insertData(Identity.getPasien());
            JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Disimpan");
            Logger.getLogger(Hasil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            frame.getMainScrollPane().setViewportView(frame.getHomePanel());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {

            String reportName = "./src/com/pras/skripsi/report/hasiltes.jrxml";
            JasperDesign jasperDesign = JRXmlLoader.load(reportName);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("NAMA", Identity.getPasien().getNama());
            map.put("TANGGAL_LAHIR", Identity.getPasien().getTanggalLahir());
            map.put("ALAMAT", Identity.getPasien().getAlamat());
            map.put("JENIS_KELAMIN", Identity.getPasien().getJenisKelamin());
            map.put("GOLONGAN_DARAH", Identity.getPasien().getGolonganDarah());
            map.put("HASIL", labHasil.getText());
            map.put("PLATES_1", (Integer) Result.getHasilAngka()[0][2] + "");
            map.put("PLATES_1_JAWABAN", (String) Result.getHasilAngka()[0][1] + "");
            map.put("PLATES_1_KUNCI", Result.getKunci()[(Integer) Result.getHasilAngka()[0][0]]);
            map.put("PLATES_2", (Integer) Result.getHasilAngka()[1][2] + "");
            map.put("PLATES_2_JAWABAN", (String) Result.getHasilAngka()[1][1] + "");
            map.put("PLATES_2_KUNCI", Result.getKunci()[(Integer) Result.getHasilAngka()[1][0]]);
            java.awt.Image image = ((ImageIcon) Result.getHasilBentuk()[0][1]).getImage();
            java.awt.Image image2 = ((ImageIcon) Result.getHasilBentuk()[1][1]).getImage();
            map.put("PLATES_26_JAWABAN", image);
            map.put("PLATES_27_JAWABAN", image2);

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JREmptyDataSource());
            JasperViewer.viewReport(jasperPrint, false);
            int a = 0;
        } catch (JRException e) {
            System.err.println(e.getMessage());

        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void labHelpMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labHelpMouseEntered
        labHelp.setForeground(Color.RED);

    }//GEN-LAST:event_labHelpMouseEntered

    private void labHelpMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labHelpMouseExited
        labHelp.setForeground(Color.BLACK);
    }//GEN-LAST:event_labHelpMouseExited

    private void labHelpMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labHelpMouseClicked
        if (labHasil.getText().equals("Normal")) {
            lazyOption(new Object[]{"Pasien memiliki mata yang Normal", "Memiliki 3 sel kerucut mata yaitu merah, hijau, biru"});
        } else if (labHasil.getText().equals("Deuteronopia / Deuteronomalia Parah") || labHasil.getText().equals("Deuteronopia / Deuteronomalia")) {
            lazyOption(new Object[]{"Pasien didiagnosa memiliki salah satu kelainan dari Deuteronopia atau Deuteronomalia Parah",
                "Deuteronopia adalah keadaan tidak adanya sel kerucut warna hijau pada mata",
                "Deuteromalia adalah keadaan menurunnya kepekaan penderita terhadap warna hijau"});
        } else if (labHasil.getText().equals("Protonopia / Protonomalia Parah") || labHasil.getText().equals("Protonopia / Protonomalia")) {
            lazyOption(new Object[]{"Pasien didiagnosa memiliki salah satu kelainan dari Protonopia / Protonomalia Parah",
                "Protonopia adalah keadaan tidak adanya sel kerucut warna merah pada mata",
                "Protonomalia adalah keadaan menurunnya kepekaan penderita terhadap warna merah"});
        } else if (labHasil.getText().equals("Deuteronomalia Ringan") || labHasil.getText().equals("Deuteronomalia")) {
            lazyOption(new Object[]{"Pasien didiagnosa memiliki kelainan Deuteronomalia",
                "Deuteromalia adalah keadaan menurunnya kepekaan penderita terhadap warna hijau"});
        } else if (labHasil.getText().equals("Protonomalia Ringan") || labHasil.getText().equals("Protonomalia")) {
            lazyOption(new Object[]{"Pasien didiagnosa memiliki kelainan dari Protonomalia",
                "Protonomalia adalah keadaan menurunnya kepekaan penderita terhadap warna merah"});
        } else {
            lazyOption(new Object[]{"Pasien didiagnosa mengidap buta warna total",
                "Buta warna total adalah keadaan dimana mata dari penderita tidak dapat",
            "membedakan warna satu dengan yang lainnya"});
        }
    }//GEN-LAST:event_labHelpMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel labGD;
    private javax.swing.JLabel labHasil;
    private javax.swing.JLabel labHelp;
    private javax.swing.JLabel labJK;
    private javax.swing.JLabel labNama;
    private javax.swing.JLabel labTanggalLahir;
    private javax.swing.JTextArea txtAlamat;
    // End of variables declaration//GEN-END:variables

    public void lazyOption(Object[] message) {
        JOptionPane.showMessageDialog(this, message, "Warning", JOptionPane.WARNING_MESSAGE);
    }

}
